package com.comyou.beacon;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.LeScanCallback;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.comyou.beacon.BeaconClass.Beacon;

import java.util.HashMap;
import java.util.Map;

public class BeaconManager {
	
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			
			int what= msg.what;
			String key=(String) msg.obj;
			
			if(monitoringBeaconRegion.containsKey(key)){
				BeaconRegion beaconRegion=monitoringBeaconRegion.get(key);
				
				beaconListener.onExitBeaconRegion(beaconRegion);
				beaconRegion.hasEntered=false;
			}
			
			
		}
		
		
		
	};
	
	private final int delayCheckDuration=10*1000;

	BluetoothAdapter bluetoothAdapter;
	
	private BeaconListener beaconListener;
	
	public void setBeaconListener(BeaconListener beaconListener) {
		this.beaconListener = beaconListener;
	}
	
	public BeaconManager(Context context) {
		BluetoothManager bluetoothManager=(BluetoothManager)context. getSystemService(Context.BLUETOOTH_SERVICE);
		
		bluetoothAdapter= bluetoothManager.getAdapter();
	}


	public void startScanBeacon(){
	
	
		bluetoothAdapter.startLeScan(leScanCallback);
		
	}
	
	
	private Map<String, BeaconRegion> monitoringBeaconRegion=new HashMap<String, BeaconRegion>();
	
	public Map<String, BeaconRegion> getMonitoringBeaconRegion() {
		return monitoringBeaconRegion;
	}
	
	int msgWhat=0;
	
	
	public void startMonitoringBeaconsInRegion(BeaconRegion region){
		String key=region.getKey();
		if(!monitoringBeaconRegion.containsKey(key)){
			msgWhat++;
			region.setMsgWhat(msgWhat);
			monitoringBeaconRegion.put(key, region);
		}
	}
	
	
	private LeScanCallback leScanCallback=new LeScanCallback() {
		
		@Override
		public void onLeScan(BluetoothDevice bluetoothdeivce, int rssi, byte[] scandata) {
			Beacon beacon=BeaconClass.fromScanData(scandata, bluetoothdeivce, rssi);
			
			if(beacon==null){
			}else{
				if(beaconListener!=null){
					beaconListener.onScanBeacon(beacon);
				}
				String key=beacon.uuid+"_"+beacon.major+"_"+beacon.minor;
				Log.e("", "key:"+key);
				if(monitoringBeaconRegion.containsKey(key)){
					BeaconRegion beaconRegion=monitoringBeaconRegion.get(key);

					handler.removeMessages(beaconRegion.getMsgWhat());
					
					Message msg=new Message();
					msg.what=beaconRegion.getMsgWhat();
					msg.obj=key;
					
					handler.sendMessageDelayed(msg, delayCheckDuration);
					
					if(!beaconRegion.hasEntered){
						Log.e(""," 进入了区域:"+key);
						beaconRegion.hasEntered=true;
						beaconListener.onEnterBeaconRegion(beaconRegion);
					}
				}
				
				key=beacon.uuid+"_-1_-1";
				if(monitoringBeaconRegion.containsKey(key)){
					BeaconRegion beaconRegion=monitoringBeaconRegion.get(key);

					handler.removeMessages(beaconRegion.getMsgWhat());
					
					Message msg=new Message();
					msg.what=beaconRegion.getMsgWhat();
					msg.obj=key;
					
					handler.sendMessageDelayed(msg, delayCheckDuration);
					
					if(!beaconRegion.hasEntered){
						Log.e(""," 进入了区域:"+key);
						beaconRegion.hasEntered=true;
						beaconListener.onEnterBeaconRegion(beaconRegion);
					}
				}
				
				key=beacon.uuid+"_"+beacon.major+"_-1";
				if(monitoringBeaconRegion.containsKey(key)){
					BeaconRegion beaconRegion=monitoringBeaconRegion.get(key);

					handler.removeMessages(beaconRegion.getMsgWhat());
					
					Message msg=new Message();
					msg.what=beaconRegion.getMsgWhat();
					msg.obj=key;
					
					handler.sendMessageDelayed(msg, delayCheckDuration);
					
					if(!beaconRegion.hasEntered){
						Log.e(""," 进入了区域:"+key);
						beaconRegion.hasEntered=true;
						beaconListener.onEnterBeaconRegion(beaconRegion);
					}
				}
			}
		}
	};
	
	
	
	
	public interface BeaconListener{
		public void onScanBeacon(Beacon beacon);
		
		public void onEnterBeaconRegion(BeaconRegion beaconRegion);
		
		public void onExitBeaconRegion(BeaconRegion beaconRegion);
		
	}
}
